home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / cpphtp2 / code.jar / code / ch03 / fig03_08.txt < prev    next >
Text File  |  1998-02-27  |  1KB  |  51 lines

  1. 1   // Fig. 3.8: fig03_08.cpp
  2. 2   // Roll a six-sided die 6000 times
  3. 3   #include <iostream.h>
  4. 4   #include <iomanip.h>
  5. 5   #include <stdlib.h>
  6. 6   
  7. 7   int main()
  8. 8   {
  9. 9      int frequency1 = 0, frequency2 = 0,
  10. 10         frequency3 = 0, frequency4 = 0,
  11. 11         frequency5 = 0, frequency6 = 0,
  12. 12         face;
  13. 13  
  14. 14     for ( int roll = 1; roll <= 6000; roll++ ) {
  15. 15        face = 1 + rand() % 6;
  16. 16  
  17. 17        switch ( face ) {
  18. 18           case 1:
  19. 19              ++frequency1;
  20. 20              break;
  21. 21           case 2:
  22. 22              ++frequency2;
  23. 23              break;
  24. 24           case 3:
  25. 25              ++frequency3;
  26. 26              break;
  27. 27           case 4:
  28. 28              ++frequency4;
  29. 29              break;
  30. 30           case 5:
  31. 31              ++frequency5;
  32. 32              break;
  33. 33           case 6:
  34. 34              ++frequency6;
  35. 35              break;
  36. 36           default:
  37. 37              cout << "should never get here!";
  38. 38        }
  39. 39     }
  40. 40  
  41. 41     cout << "Face" << setw( 13 ) << "Frequency"
  42. 42          << "\n   1" << setw( 13 ) << frequency1
  43. 43          << "\n   2" << setw( 13 ) << frequency2
  44. 44          << "\n   3" << setw( 13 ) << frequency3
  45. 45          << "\n   4" << setw( 13 ) << frequency4
  46. 46          << "\n   5" << setw( 13 ) << frequency5
  47. 47          << "\n   6" << setw( 13 ) << frequency6 << endl;
  48. 48  
  49. 49     return 0;
  50. 50  } 
  51.